//
//  ActivityViewController.m
//  UI14_豆瓣电影更新版
//
//  Created by dllo on 15/12/31.
//  Copyright © 2015年 lanou3g. All rights reserved.
//

#import "ActivityViewController.h"
#import "ActivityTableViewCell.h"
#import "ActivityModel.h"
#import "UIImageView+WebCache.h"
#import "DetailActivityViewController.h"
#import "dataBaseHandle.h"
@interface ActivityViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) NSMutableArray *arrData;
@property (nonatomic, retain) NSMutableArray *arrModel;
@end

@implementation ActivityViewController
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"bg_nav.png"] forBarMetrics:UIBarMetricsDefault];
    }
    return self;
}
- (void)dealloc {
    [_arrModel release];
    [_arrData release];
    [_tableView release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"活动";
    [self dataHandle];
}
// 行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _arrModel.count;
}

// cell赋值
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ActivityTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"activityCell"];
    
    ActivityModel *model = _arrModel[indexPath.row];
    NSString *str = model.image;
    [cell.img3 sd_setImageWithURL:[NSURL URLWithString:str]];
    
    cell.nameLabel.text = model.title;
    // 时间
    NSString *str2 = [model.begin_time substringWithRange:NSMakeRange(5, 11)];
    NSString *str3 = [model.end_time substringWithRange:NSMakeRange(5, 11)];
    NSString *str4 = [NSString stringWithFormat:@"%@--%@", str2, str3];
    cell.dataLabel.text = str4;
    // 地址
    cell.spotLabel.text = model.address;
    // 类型
    cell.catalogLabel.text = model.category_name;
    cell.label1.text = @"感兴趣:";
    cell.label3.text = @"参加:";
    cell.label2.text = [NSString stringWithFormat:@"%@", model.wisher_count];
    cell.label2.textColor = [UIColor redColor];
    cell.label4.text = [NSString stringWithFormat:@"%@", model.participant_count];
    cell.label4.textColor = [UIColor redColor];
    
    
    return cell;
}

// 返回行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 623 / 3;
}

// 点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // 创建表
    [[dataBaseHandle sharedDataBaseHandle] creatTable];
    // 跳转  传值
    DetailActivityViewController *detail = [[DetailActivityViewController alloc] init];
    ActivityModel *model = _arrModel[indexPath.row];
    detail.str = model.title;
    detail.model = model;
    detail.arrDB = _arrModel;
    
    [self.navigationController pushViewController:detail animated:NO];
    [detail release];
}



- (void)dataHandle {
    // tableView铺设
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 49) style:UITableViewStylePlain];
    [self.view addSubview:_tableView];
    [_tableView release];
    // 设置代理人
    _tableView.delegate = self;
    _tableView.dataSource = self;
    // 重用池
    [_tableView registerClass:[ActivityTableViewCell class] forCellReuseIdentifier:@"activityCell"];
    // 数据解析
    NSString *str = [[NSBundle mainBundle] pathForResource:@"activitylist-2" ofType:@"txt"];
    //    NSString *newStr = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSData *data = [NSData dataWithContentsOfFile:str];
    NSMutableDictionary *result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
    //    NSLog(@"%@", result);
    
    self.arrData = result[@"events"];
    self.arrModel = [NSMutableArray array];
    for (NSMutableDictionary *dic in _arrData) {
        ActivityModel *model = [ActivityModel activity];
        [model setValuesForKeysWithDictionary:dic];
        [_arrModel addObject:model];
    }

}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
